/*
 * AddClass.cpp
 *
 *  Created on: Jan 11, 2009
 *      Author: sez
 */

#include "AddClass.h"
#include "ui_addclass.h"
#include "AddPower.h"
#include "AddTrait.h"

#include <DndData/ClassDetails.h>
#include <Models/DataListModel.h>
#include <Models/TraitListModel.h>

struct ClassModels : public DialogModels
{
    ClassModels()
        : DialogModels()
        , mClassSkillsModel(0)
        , mFixedSkillsModel(0)
        , mBuildOptions(0)
        , mClassFeatures(0)
        , mTraitsModel(0)
        , mPowersModel(0)
    {
    }
    ~ClassModels()
    {
        delete mClassSkillsModel;
        delete mFixedSkillsModel;
        delete mBuildOptions;
        delete mClassFeatures;
        delete mTraitsModel;
        delete mPowersModel;
    }
    SkillsModel *mClassSkillsModel;
    SkillsModel *mFixedSkillsModel;
    TraitModel *mBuildOptions;
    TraitModel *mClassFeatures;
    TraitModel *mTraitsModel;
    PowerModel *mPowersModel;
};

AddClass::AddClass(QWidget *parent)
    : AddUpdateDialog(XmlPath(XmlPath::CLASS_BASE), parent)
    , mAddClass(new Ui::AddClassDialog())
    , mData(0)
    , mModels(new ClassModels())
{
    mAddClass->setupUi(this);
}

AddClass::~AddClass()
{
    delete mAddClass;
    delete mData;
    delete mModels;
}

/*!
  \brief Return the instance of the AddClass dialog.
  This method is first called during the construction of the QMainWindow,
  and at this time the instance is initialized with the main window as its
  \a parent.  With this invariant, the method is thread-safe, otherwise it
  is not.  On subsequent calls the already initialized instance is returned
  and any argument is ignored.
 */
AddClass *AddClass::dialog(QWidget *parent)
{
    static AddClass *dlg = 0;
    if (dlg == 0)
        dlg = new AddClass(parent);
    return dlg;
}

TitledData *AddClass::data()
{
    if (!mData)
        mData = new ClassDetails(xmlPath().typeName());
    return mData;
}

void AddClass::setupData()
{
    mAddClass->roleComboBox->clear();
    for (int i = 0; i < DndData::NUM_PARTY_ROLES; ++i)
        mAddClass->roleComboBox->addItem(DndData::ROLE_NAMES[i]);
    mAddClass->roleComboBox->setCurrentIndex(0);
    mAddClass->powerSourceComboBox->clear();
    for (int i = 0; i < Power::NUM_POWER_SOURCES; ++i)
        mAddClass->powerSourceComboBox->addItem(Power::POWER_SOURCE_NAMES[i]);
    mAddClass->powerSourceComboBox->setCurrentIndex(0);

    if (!mModels->mInitialized)
    {
        mModels->mClassSkillsModel = makeSkillsModel(this);
        mModels->mFixedSkillsModel = makeSkillsModel(this);
        mAddClass->classSkillsListView->setModel(mModels->mClassSkillsModel);
        mAddClass->fixedSkillsListView->setModel(mModels->mFixedSkillsModel);
        mModels->mInitialized = true;
    }

    delete mModels->mBuildOptions;
    mModels->mBuildOptions = makeTraitModel(xmlPath(), this);
    mAddClass->buildOptionsListView->setModel(mModels->mBuildOptions);
    delete mModels->mClassFeatures;
    mModels->mClassFeatures = makeTraitModel(xmlPath(), this);
    mAddClass->buildOptionsListView->setModel(mModels->mClassFeatures);
    delete mModels->mPowersModel;
    mModels->mPowersModel = makePowerModel(xmlPath(), this);
    mAddClass->buildOptionsListView->setModel(mModels->mPowersModel);
    delete mModels->mTraitsModel;
    mModels->mTraitsModel = makeTraitModel(xmlPath(), this);
    mAddClass->buildOptionsListView->setModel(mModels->mTraitsModel);
}

void AddClass::saveData()
{
}

void AddClass::loadData()
{
}

void AddClass::clearData()
{
}

void AddClass::on_powerPushButton_clicked(bool)
{
    XmlPath xp(xmlPath());
    xp.setSubType(XmlPath::POWER_TYPE);
    AddPower *dlg = AddPower::dialog();
    dlg->setXmlPath(xp);
    dlg->show();
    dlg->raise();
    dlg->activateWindow();
}

void AddClass::on_traitPushButton_clicked(bool)
{
    XmlPath xp(xmlPath());
    xp.setSubType(XmlPath::TRAIT_TYPE);
    AddTrait *dlg = AddTrait::dialog();
    dlg->setXmlPath(xp);
    dlg->show();
    dlg->raise();
    dlg->activateWindow();
}
